package com.yzk.auction.service;

import com.yzk.auction.dao.UserDao;
import com.yzk.auction.entity.ShowOrder;
import com.yzk.auction.entity.Users;

import java.util.List;

/**
 * Author: 园中葵小组成员
 * Description:用户相关操作服务类
 *             对UserDao进行调用，对代码进行前置检查，或后置结果处理
 */
public class UserService {
    private UserDao userDao = new UserDao();
    public Users login(Users users){
        Users u = userDao.findByTelPwd(users.getUser_tel(),users.getUser_pwd());
        System.out.println(u);
        return u;
    }
    public boolean regist(Users users){
        //1.根据用户名查询用户对象
        Users u = userDao.findByTel(users.getUser_tel());
        //判断u是否为Null
        if(u!=null){
            return false;
        }
        //2.保存用户信息
        userDao.save(users);
        return true;
    }

    /**
     * 通过手机号查询
     * @param telephone
     * @return
     */
    public Users findByTel(String telephone){
        return userDao.findByTel(telephone);
    }

    /**
     * 根据Id查询用户
     * @param id
     * @return
     */
    public Users findById(Integer id){
        return userDao.findById(id);
    }
    /**
     * 更新用户信息
     * @param newUsers
     * @param isPictureModified
     */
    public Integer update(Users newUsers,int isPictureModified){
        Users oldUsers = this.findById(newUsers.getUser_id());
        oldUsers.setUser_name(newUsers.getUser_name());
        oldUsers.setUser_sex(newUsers.getUser_sex());
        oldUsers.setUser_tel(newUsers.getUser_tel());
        oldUsers.setUser_addr(newUsers.getUser_addr());
        if(isPictureModified == 1){
            oldUsers.setUser_head(newUsers.getUser_head());
        }
        int num = userDao.update(oldUsers);
        return num;
    }

    /**
     * 根据用户查询所有订单
     * @param id
     * @return
     */
    public List<ShowOrder> findOrderById(Integer id){
        return userDao.findOrderById(id);
    }
}
